package colab.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import colab.dominio.Usuario;
import colab.infraestrutura.ColabException;
import colab.infraestrutura.ColabRuntimeException;
import colab.negocio.UsuarioBS;
import colab.persistencia.UsuarioDAO;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

public class TelaCadastroPorAdm extends JFrame {

	private JPanel contentPane;
	private JTextField textCPF;
	private JLabel lblSenha;
	private JTextField textSenha;
	private JLabel lblConfirmaSenha;
	private JTextField textConfirmaSenha;
	private JTextField textNome;
	private Usuario usuario=new Usuario();
	private JButton btnVoltar;
	
	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public TelaCadastroPorAdm() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\workspace\\CO-LAB\\src\\colab\\gui\\imagens\\COLAB-001.PNG"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 564, 453);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCpf = new JLabel("Login/CPF");
		lblCpf.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCpf.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCpf.setBounds(96, 140, 81, 20);
		contentPane.add(lblCpf);
		
		textCPF = new JTextField();
		textCPF.setBounds(203, 142, 152, 20);
		contentPane.add(textCPF);
		textCPF.setColumns(10);
		
		lblSenha = new JLabel("Senha");
		lblSenha.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSenha.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblSenha.setBounds(131, 186, 46, 19);
		contentPane.add(lblSenha);
		
		textSenha = new JTextField();
		textSenha.setColumns(10);
		textSenha.setBounds(203, 187, 152, 20);
		contentPane.add(textSenha);
		
		lblConfirmaSenha = new JLabel("Confirma senha");
		lblConfirmaSenha.setHorizontalAlignment(SwingConstants.RIGHT);
		lblConfirmaSenha.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblConfirmaSenha.setBounds(65, 263, 111, 19);
		contentPane.add(lblConfirmaSenha);
		
		textConfirmaSenha = new JTextField();
		textConfirmaSenha.setColumns(10);
		textConfirmaSenha.setBounds(203, 264, 152, 20);
		contentPane.add(textConfirmaSenha);
		
		final JComboBox comboBoxTipo = new JComboBox();
		comboBoxTipo.setBackground(Color.WHITE);
		comboBoxTipo.setToolTipText("1=Aluno\r\n2=Administrador\r\n3=Professor");
		comboBoxTipo.setModel(new DefaultComboBoxModel(new String[] {"Administrador","Aluno", "Professor"}));
		comboBoxTipo.setBounds(203, 233, 152, 20);
		contentPane.add(comboBoxTipo);
		
		JLabel labelTipo = new JLabel("Tipo de usu\u00E1rio");
		labelTipo.setHorizontalAlignment(SwingConstants.RIGHT);
		labelTipo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		labelTipo.setBounds(66, 229, 111, 23);
		contentPane.add(labelTipo);
		
		JLabel lblCadastroUsurio = new JLabel("Cadastro Usu\u00E1rio");
		lblCadastroUsurio.setHorizontalAlignment(SwingConstants.LEFT);
		lblCadastroUsurio.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCadastroUsurio.setBounds(193, 25, 152, 20);
		
		
		textNome = new JTextField();
		textNome.setColumns(10);
		textNome.setBounds(203, 103, 152, 20);
		contentPane.add(textNome);
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNome.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNome.setBounds(96, 101, 81, 20);
		contentPane.add(lblNome);
		
		JButton btnCadastrar = new JButton("Cadastrar");
		btnCadastrar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
					
				UsuarioBS usuarioBS = UsuarioBS.getInstance();

				
				
				try {
					usuario.setCpf(usuarioBS.converteCPF(textCPF.getText()));
				
				usuario.setNome(textNome.getText());
				usuario.setSenha(usuarioBS.confirmaSenha(textSenha.getText(), textConfirmaSenha.getText()));
				usuario.setTipo(comboBoxTipo.getSelectedIndex()+1);				
				usuarioBS.inserirUsuario(usuario);
				
				JOptionPane.showMessageDialog(null, "Cadastrado");
				} catch (ColabException| SQLException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage());
				
				}
						
		}
				
				
			
			
			
			
		});
		btnCadastrar.setBackground(Color.WHITE);
		btnCadastrar.setBounds(228, 295, 100, 23);
		contentPane.add(btnCadastrar);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon("C:\\workspace\\CO-LAB\\src\\colab\\gui\\imagens\\COLAB-001-peq.PNG"));
		label.setBounds(10, 11, 85, 47);
		contentPane.add(label);
		
		btnVoltar = new JButton("Voltar");
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new TelaAdm().setVisible(true);
				dispose();
			}
		});
		btnVoltar.setBounds(10, 381, 89, 23);
		contentPane.add(btnVoltar);
	}
}
